package top.ucat.x.system.controller.provider;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ucat.box.cloud.common.result.Result;
import top.ucat.box.cloud.common.result.SystemResult;
import top.ucat.box.starter.oauth.common.pojo.User;
import top.ucat.box.starter.oauth.common.util.UserUtil;
import top.ucat.x.system.entity.SysRoleGroup;
import top.ucat.x.system.entity.beans.UserInfoDto;
import top.ucat.x.system.entity.beans.menu.MenuTreeVo;
import top.ucat.x.system.entity.beans.role.RoleTreeVo;
import top.ucat.x.system.service.management.SysMenuService;
import top.ucat.x.system.service.management.SysRoleGroupService;
import top.ucat.x.system.service.management.SysRoleService;
import top.ucat.x.system.service.management.SysUserService;

import java.util.List;

/**
 * @Auther: Jun
 * @Date: 2020/4/18 17:19
 * @Description:
 */
@RestController
@RequestMapping("public")
@Slf4j
public class PublicController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleGroupService sysRoleGroupService;

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/user/info")
    public Result<UserInfoDto> getUserInfo() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserInfoDto userInfoDto = sysUserService.getUserInfoDto(user.getId());
        return SystemResult.OK.getResult(userInfoDto);
    }

    @GetMapping("/user/menu/top")
    public Result getTopmenu() {
        User user = UserUtil.getUser();
        List<MenuTreeVo> topMenus = sysMenuService.getTopMenuTrees(user.getId());
        return SystemResult.OK.getResult(topMenus);
    }

    @GetMapping("/user/menu")
    public Result getUserMenu(String id) {
        User user = UserUtil.getUser();
        List<MenuTreeVo> userMenu = sysMenuService.getUserMenuTrees(user.getId(), id);
        return SystemResult.OK.getResult(userMenu);
    }

    @GetMapping("menu/all/tree")
    public List<MenuTreeVo> getAllMenuTree() {
        List<MenuTreeVo> allMenuTree = sysMenuService.getAllMenuTrees();
        return allMenuTree;
    }

    @GetMapping("role/tree")
    public Result getRoleTree() {
        List<RoleTreeVo> roleTrees = sysRoleService.getRoleTrees();
        return SystemResult.OK.getResult(roleTrees);
    }

    @GetMapping("role/group/all")
    public List<SysRoleGroup> getAllRoleGroup() {
        List<SysRoleGroup> list = sysRoleGroupService.getAllGroup();
        return list;
    }

    @GetMapping("permission/tree")
    public Result getPermissionTree(int type) {
        List<MenuTreeVo> allMenuTree = sysMenuService.getAllMenuTrees();
        return SystemResult.OK.getResult(allMenuTree);
    }


}
